package com.lcn.controller;

import com.lcn.base.BaseInfoProperties;
import com.lcn.grace.result.GraceJSONResult;
import com.lcn.pojo.bo.FriendCircleBO;
import com.lcn.pojo.vo.FriendCircleVO;
import com.lcn.service.CommentService;
import com.lcn.service.FriendCircleService;
import com.lcn.utils.PagedGridResult;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/friendCircle")
public class FriendCircleController extends BaseInfoProperties {

    @Resource
    private FriendCircleService friendCircleService;

    @Resource
    private CommentService commentService;

    /**
     * 发布朋友圈
     *
     * @param friendCircleBO
     * @return
     */
    @PostMapping("/publish")
    public GraceJSONResult publish(@RequestBody FriendCircleBO friendCircleBO, HttpServletRequest request) {
        String userId = request.getHeader(HEADER_USER_ID);
        friendCircleBO.setUserId(userId);
        friendCircleBO.setPublishTime(LocalDateTime.now());
        friendCircleService.save(friendCircleBO);
        return GraceJSONResult.ok();
    }

    /**
     * 分页查询朋友圈列表
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @PostMapping("/queryList")
    public GraceJSONResult queryList(String userId,
                                     @RequestParam(defaultValue = "1", name = "page") Integer page,
                                     @RequestParam(defaultValue = "10", name = "pageSize") Integer pageSize) {
        if (StringUtils.isBlank(userId)) {
            return GraceJSONResult.error();
        }
        PagedGridResult pagedGridResult = friendCircleService.queryList(userId, page, pageSize);
        List<FriendCircleVO> friendCircleVOS = ( List<FriendCircleVO> ) pagedGridResult.getRows();
        for (FriendCircleVO friendCircleVO : friendCircleVOS) {
            String friendCircleId = friendCircleVO.getFriendCircleId();
            friendCircleVO.setLikedFriends( friendCircleService.queryFriendCircleLikedList( friendCircleId ) );
            friendCircleVO.setDoILike(friendCircleService.doILike(friendCircleId, userId));
            friendCircleVO.setCommentList( commentService.queryComment(friendCircleId) );
        }
        return GraceJSONResult.ok(pagedGridResult);
    }

    /**
     * 点赞
     *
     * @param friendCircleId
     * @return
     */
    @PostMapping("/like")
    public GraceJSONResult like(String friendCircleId, HttpServletRequest request) {
        String userId = request.getHeader(HEADER_USER_ID);
        if (StringUtils.isBlank(userId)) {
            return GraceJSONResult.error();
        }
        friendCircleService.like(friendCircleId, userId);
        return GraceJSONResult.ok();
    }

    /**
     * 取消点赞
     *
     * @param friendCircleId
     * @return
     */
    @PostMapping("/unlike")
    public GraceJSONResult unlike(String friendCircleId, HttpServletRequest request) {
        String userId = request.getHeader(HEADER_USER_ID);
        if (StringUtils.isBlank(userId)) {
            return GraceJSONResult.error();
        }
        friendCircleService.unlike(friendCircleId, userId);
        return GraceJSONResult.ok();
    }

}
